package com.jdcjk.project.setting.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jdcjk.R;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.project.setting.PositionReportService;
import com.jdcjk.project.setting.presenter.SettingPresenter;
import com.jdcjk.util.PreferencesUtils;

/**
 * Created by yu on 2017/3/21 0021.
 * 位置上报信息设置界面
 */

public class PositionReportSettingActivity extends BaseActivity<SettingPresenter> implements View.OnClickListener {
    private final String TAG = PositionReportSettingActivity.class.getSimpleName();
    //确定按钮textview
    private TextView determineBtn;
    //返回按钮
    private ImageButton backBtn;
    //位置上报开关switchView
//    private SwitchView positionReportSwitchVie;
    //位置上报开关加载条
//    private ImageView iLoadPushImageView;
    //上报手机号码EditText
    private EditText reportPhoneNumberEt;
    //车牌号码editText
    private EditText licensePlateNumberEt;
    //上报时间频度editText
    private EditText reportTimeFrequencyEt;
    //是否开启位置上报
    private boolean isPush;

    @Override
    public int getLayoutId() {
        return R.layout.activity_position_report_setting;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {
        backBtn = (ImageButton) findViewById(R.id.back_btn);
        backBtn.setOnClickListener(this);
        determineBtn = (TextView) findViewById(R.id.position_report_setting_determine_btn);
        determineBtn.setOnClickListener(this);
        reportPhoneNumberEt = (EditText) findViewById(R.id.report_phone_number_et);
        licensePlateNumberEt = (EditText) findViewById(R.id.license_plate_number_et);
        reportTimeFrequencyEt = (EditText) findViewById(R.id.report_time_frequency_et);

        //获取设定好的车牌号和上报频度
        String reportPhoneNumber = PreferencesUtils.getString(this, PreferencesUtils.REPORT_PHONE_NUMBER);
        String licensePlatNumber = PreferencesUtils.getString(this, PreferencesUtils.LICENSE_PLATE_NUMBER);
        String reportTimeFrequency = PreferencesUtils.getString(this, PreferencesUtils.REPORT_TIME_FREQUENCY);
        reportPhoneNumberEt.setText(reportPhoneNumber);
        licensePlateNumberEt.setText(licensePlatNumber);
        reportTimeFrequencyEt.setText(reportTimeFrequency);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.position_report_setting_determine_btn:
                //确定按钮点击监听
                String reportPhoneNumber = reportPhoneNumberEt.getText().toString();
                String licensePlateNumber = licensePlateNumberEt.getText().toString();
                String reportTimeFrequency = reportTimeFrequencyEt.getText().toString();
                //判断是否有设置
                if (licensePlateNumber.equals("") || reportTimeFrequency.equals("") || reportPhoneNumber.equals("")) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                PreferencesUtils.setString(this, PreferencesUtils.REPORT_PHONE_NUMBER, reportPhoneNumber);
                PreferencesUtils.setString(this, PreferencesUtils.REPORT_TIME_FREQUENCY, reportTimeFrequency);
                PreferencesUtils.setString(this, PreferencesUtils.LICENSE_PLATE_NUMBER, licensePlateNumber);
                //修改后重启service
                stopService(new Intent(PositionReportSettingActivity.this, PositionReportService.class));
                startService(new Intent(PositionReportSettingActivity.this, PositionReportService.class));
                finish();
                break;
            case R.id.back_btn:
                finish();
                break;
        }
    }

}
